const MongoClient = require('mongodb').MongoClient;
const url = "mongodb://taosha_user:apyl8XWP6LSW4JHs@101.35.6.34:27722/taosha";

async function main() {
  const client = await MongoClient.connect(url, {useNewUrlParser: true, useUnifiedTopology: true});
  const db = client.db('taosha');

  let topics = await db.collection('topics')
    .aggregate([
      {$match: {original_url: {$exists: true}}},
      {$group: {_id: {content: '$original_url'}, count: {$sum: 1}, dups: {$addToSet: '$_id'}}},
      {$match: {count: {$gt: 1}}},
      {$sort: {"create_at": 1}}
    ])
    .toArray();

  console.log(topics);
  topics.forEach(async function (doc) {
    // shift是删除先加入的文章
    doc.dups.shift();

    // pop是删除后加入的文章
    // doc.dups.pop();

    let result = await db.collection('topics').deleteOne({_id: {$in: doc.dups}});
    console.log(result.result);
  });

}

main();
